﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GameLogic.Controls;
using GameLogic.Logic;

namespace NumberGuessing
{
    public partial class NumberGuessingGame : UserControl, IGameFeatures
    {
        // IGameFeatures events
        //public event EventHandler<HighscoreEntryArgs> HighscoreEntryFired;
        public event EventHandler<ScoresChangedEventArgs> ChangeScoresFired;
        public event EventHandler<LivesChangedEventArgs> ChangeLivesFired;
        public event EventHandler PauseTimerFired;
        public event EventHandler ResumeTimerFired;
        public event EventHandler GameOverFired;
        public event EventHandler NewGameFired;

        // Game values
        Random random = new Random();
        int numberRange; // Range of random numbers (e.g. 100 -> 1 to 100)
        int guessNumber; // Current number, player has to guess
        bool highscoreEntry; // Was player good enough for a highscore entry
              
        public NumberGuessingGame()
        {
            InitializeComponent();
        }

        //
        // Level Selection
        //
        private void _level1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            numberRange = 10;
            this.StartGame(numberRange);
        }      

        private void _level2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            numberRange = 100;
            this.StartGame(numberRange);
        }

        private void _level3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            numberRange = 1000;
            this.StartGame(numberRange);
        }

        // 
        // Game Logic
        //
        private void _enter_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int userGuess;

            if (int.TryParse(_input.Text, out userGuess))
            {
                if (userGuess > guessNumber)
                {
                    _status.Text = "Falsch! Die geratene Zahl ist zu hoch.";
                    this.ChangeScores(-5);
                }
                else if (userGuess < guessNumber)
                {
                    _status.Text = "Falsch! Die geratene Zahl ist zu niedrig.";
                    this.ChangeScores(-5);
                }
                else
                {
                    _status.Text = "Richtig! Das war die gesuchte Zahl!";
                    this.ChangeScores(numberRange);
                    
                    // End game
                    this.EndGame();
                }
            }
        }
       
        private void StartGame(int maxValue)
        {
            highscoreEntry = false;

            // Go to game screen
            VisualStateManager.GoToState(this, "Game", true);
            _input.Text = "";

            // Set number to be guessed
            guessNumber = random.Next(maxValue) + 1;

            // Update text
            _challenge.Text = String.Format("Rate eine Zahl zwischen 1 und {0}", numberRange);
            _status.Text = "Bitte gib eine Zahl ein";  
        }

        private void EndGame()
        {
            // Change lives to 0 so the game ends
            this.ChangeLives(-1);
        }

        //private void _newGame_Click(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    // If player was good enough for highscore entry
        //    if (highscoreEntry && this.HighscoreEntryFired != null)
        //    {
        //        HighscoreEntryArgs args = new HighscoreEntryArgs();
        //        args.Player = _playerName.Text;
        //        this.HighscoreEntryFired(this, args);
        //    }
        //    // Start new game
        //    if (this.NewGameFired != null)
        //    {
        //        this.NewGameFired(this, EventArgs.Empty);
        //    }
        //}

        //
        // IGameFeatures Members
        //
        public void Start()
        {
            // Go to level selection
            VisualStateManager.GoToState(this, "LevelSelection", false);
        }   

        public void GameOver(GameOverEventArgs args)
        {
            // Tell game container that game is over
            if (this.GameOverFired != null)
            {
                this.GameOverFired(this, EventArgs.Empty);
            }

            // Show congratulation message
            VisualStateManager.GoToState(this, "HighscoreEntry", true);
            
            _highscoreComment.Visibility = Visibility.Collapsed;
            _playerName.Visibility = Visibility.Collapsed;

            // If player was good enough for the highscore list 
            if (args.HighscoreRank > 0)
            {
                // Ask for name
                _highscoreComment.Visibility = Visibility.Collapsed;
                _playerName.Visibility = Visibility.Collapsed;

                _highscoreComment.Text = String.Format(
                    "Du hast es auf Rang {0} der Highscore-Liste geschafft!\nBitte gib deinen Namen ein",
                    args.HighscoreRank);

                highscoreEntry = true;
            }
        }

        public void ChangeScores(int p)
        {
            if (ChangeScoresFired != null)
            {
                ScoresChangedEventArgs args = new ScoresChangedEventArgs();
                args.Value = p;
                ChangeScoresFired(this, args);
            }
        }

        public void ChangeLives(int p)
        {
            if (ChangeLivesFired != null)
            {
                LivesChangedEventArgs args = new LivesChangedEventArgs();
                args.Value = p;
                ChangeLivesFired(this, args);
            }
        }

        // We don't need these
        public void Stop() { }

        public void Pause() { }

        public void Resume() { }


        public bool IsTimeBased
        {
            get { return false; }
        }

        public bool IsLifeBased
        {
            get { return false; }
        }

        public string GameName
        {
            get { return "Number Guessing"; }
        }


        public TimeSpan GameDuration
        {
            get { throw new NotImplementedException(); }
        }
    }
}
